import { ref } from 'vue';
import useRouter from '@/hooks/useRouter';
import { getLawyerRole } from '@/api';
import { onLoad } from '@dcloudio/uni-app';
export default function useRegister() {
	const router = useRouter();
	// 表单数据对象
	const formData = ref({
		// phone: '17712152745', // 电话
		// yzm: '1', // 验证码
		// password: '123456', // 密码
		// repassword: '123456', // 确认密码
		// name: '李四', // 姓名
		// role: '', // 角色
		// certificate: '123456', // 执业证号码
		// address: '北京市市辖区东城区' // 地址
		is_haved: 0,

		phone: '', // 电话
		yzm: '', // 验证码
		password: '', // 密码
		repassword: '', // 确认密码
		name: '', // 姓名
		role: '', // 角色
		certificate: '', // 执业证号码
		address: '' // 地址
	} as any);
	const formRules = ref({
		phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }], // 手机号校验
		password: [{ required: true, message: '请输入密码', trigger: 'blur' }], // 密码
		repassword: [
			{
				required: true,
				message: '请输入确认密码',
				trigger: 'blur',
				validator: (value : any) => {
					if (value !== formData.value.password) {
						return Promise.reject('确认密码与密码不一致');
					}
					return Promise.resolve();
				}
			}
		], // 确认密码
		yzm: [{ required: true, message: '请输入验证码', trigger: 'blur' }] // 验证码
	});
	const formRules2 = {
		name: [{ required: true, message: '请输入姓名', trigger: 'blur' }], // 姓名
		role: [{ required: true, message: '请选择角色', trigger: 'blur' }], // 角色
		certificate: [{ required: true, message: '请输入执业证号码', trigger: 'blur' }], // 执业证号码
		address: [{ required: true, message: '请输入地址', trigger: 'blur' }] // 地址
	};
	// 角色列表
	const columns = ref([]);
	// 角色选择
	const handleConfirm = ({ value }) => {
		formData.value.role = value;
	};
	// 地区选择
	const columnCount = ref(3);
	const defaultValue = ref('');
	const visible = ref(false);
	const openCityPicker = () => {
		console.log('111');
		visible.value = true;
	};

	const protocol = ref(false); // 协议勾选状态
	// 协议勾选状态切换方法
	const checkboxChange = (e : { value : boolean }) => {
		protocol.value = e.value;
	};
	// 前往协议页面
	const goProtocolPage = () => { };
	// 当前步骤
	const step = ref(1);
	// 去登陆
	const goLogin = () => {
		router.redirect('/commonPage/login/login');
	};
	// 返回上一步
	const perStep = () => {
		step.value = 1;
	};

	const confirm = (res) => {
		console.log(res);
		formData.value.address = res.name;
		visible.value = false; // 关闭弹框
	};
	const cancel = () => {
		visible.value = false; // 关闭弹框
	};
	//TODO 微信小程序 渲染
	const pickerChange = (e) => {
		formData.value.address = e.detail.value.join()
		console.log(e.detail.value); // 获取选择的值
	};

	onLoad(() => {
		// 获取角色数据
		getLawyerRole().then(res => {
			columns.value = res;
			console.log(columns)
		})
	})
	return {
		columns,
		handleConfirm,
		columnCount,
		defaultValue,
		visible,
		openCityPicker,
		protocol,
		checkboxChange,
		step,
		goLogin,
		perStep,
		confirm,
		cancel,
		formData,
		formRules,
		formRules2,
		pickerChange,
		router
	};
}